/**
 * 忘记密码
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/5
 */
var self = home_info = '';

document.addEventListener('plusready' , ready ,  false);

function ready(){
	self = plus.webview.currentWebview();
	
	refresh();
	
	updateData();
	
	info()
}

function refresh(){
	self.setPullToRefresh({support:true,
		height:"50px",
		range:"200px",
		contentdown:{  
			caption:"下拉可以刷新"
		},
		contentover:{
			caption:"释放立即刷新"
		},
		contentrefresh:{
			caption:"正在刷新..."
		} 
	},function(){
		setTimeout(function(){
			info();
			self.endPullToRefresh();
		},1500);
	});
	
}

//获取首页数据
function info(){
	
	plus.nativeUI.showWaiting('加载中...');
	
	$.ajax({
		
		type:"post",
		
		url:apiRoot + '/Home/Article/articleInfo/',
		
		success : function(data){
			
			plus.nativeUI.closeWaiting();
			
			plus.storage.setItem('home_info' , data);
			
			home_info = plus.storage.getItem('home_info');
			
			showInfo(false);
			 
		},
		
		error : function(e){
			toast('连接超时 ,请检查网络');
			console.log(JSON.stringify(e));
			plus.nativeUI.closeWaiting();
		}
		
	});
}


/**
 * 更新用户的数据 
 */
function updateData(){
	var memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo);
	$.ajax({
		type:"post", 
		url:apiRoot + '/Home/Member/updataDate/',
		data:{uid : memberInfo.aid},
		success : function(data){}
	});
}


//数据显示在页面
function showInfo(type){
	
	if(type){
		
		plus.nativeUI.showWaiting('加载中...'); 
		
		setTimeout(function(){ 
			plus.nativeUI.closeWaiting();
		} , 1000);
		
	}
	
	
	home_info = $.parseJSON(home_info);
	
	//轮播图
	 
	if(home_info.carousel){
		
		var	pics = point ='';  
		
		pics += '<div data-title="'+home_info.carousel[(home_info.carousel.length-1)].title+'" class="mui-slider-item mui-slider-item-duplicate"><div class="content" style="display:none">'+home_info.carousel[(home_info.carousel.length-1)].content+'</div><a><img src="'+getImgUrl(home_info.carousel[(home_info.carousel.length-1)].pic)+'" style="width: 100%;height: 181px;"></a></div>';
		
		$.each(home_info.carousel , function(k,v) { 
			pics += '<div data-title="'+v.title+'" class="mui-slider-item"><div class="content" style="display:none">'+v.content+'</div><a><img src="'+getImgUrl(v.pic)+'" style="width: 100%;height: 181px;"></a></div>';
			if(k == 0){
				point += '<div class="mui-indicator mui-active"></div>';
			}else{
				point += '<div class="mui-indicator"></div>'; 
			}
		});
		 
		pics += '<div data-title="'+home_info.carousel[0].title+'" class="mui-slider-item mui-slider-item-duplicate"><div class="content" style="display:none">'+home_info.carousel[0].content+'</div><a><img src="'+getImgUrl(home_info.carousel[0].pic)+'" style="width: 100%;height: 181px;"></a></div>';
		
		$(".mui-slider-loop").html(pics);
		$(".mui-text-right").html(point);   
		$(".mui-text-right").after('<script>mui("#slider").slider({interval: 2000});</script>');
		
		$('.mui-slider-item').unbind();
		$('.mui-slider-item').on('click' , function(){
			plus.nativeUI.showWaiting();
			var ads = plus.webview.create('ads-inf.html' , 'ads' , {} , {title : $(this).attr('data-title') , body : $(this).find('.content').html()})
			ads.addEventListener('loaded' , function(){
				ads.show('pop-in' , 300);
				plus.nativeUI.closeWaiting();
			})
		})
	}
	
	//使用规则
	if(home_info.use_rule){
		$('.use_rule').on('click' , function(){
			plus.nativeUI.showWaiting('跳转中...');
			var use_rule =  plus.webview.create('user-text.html' , 'user-text' , {} , {body : home_info.use_rule});
			use_rule.addEventListener('loaded' , function(){
				use_rule.show('pop-in'); 
				plus.nativeUI.closeWaiting();
			})
		})
		
	}
	 
	//热门活动
	if(home_info.hot_activity){
		$('.hot_activity').each(function(index){
			$(this).find('span').text(home_info.hot_activity[index].title)
			$(this).find('img').attr('src' , getImgUrl(home_info.hot_activity[index].litpic));
			$(this).on('click' , function(){
				plus.nativeUI.showWaiting('跳转中...');
				var hd_inf = plus.webview.create('hd-inf.html' , 'hd-inf' , {} , {tid : home_info.hot_activity[index].tid, phone:home_info.hot_activity[index].phone,  title : home_info.hot_activity[index].title , body : home_info.hot_activity[index].body , aid : home_info.hot_activity[index].id});
				hd_inf.addEventListener('loaded' , function(){
					hd_inf.show('pop-in'); 
					plus.nativeUI.closeWaiting();
				})
			})
		})
	}
	
	//合作伙伴
	if(home_info.cooperative_partner){
		
		$('.mui-col-xs-6').each(function(index){
			
			$(this).find('span').text(home_info.cooperative_partner[index].title)
			
			$(this).find('img').attr('src' , getImgUrl(home_info.cooperative_partner[index].litpic))
			
			$(this).on('click' , function(){
				plus.nativeUI.showWaiting('跳转中...');
				var partner_inf = plus.webview.create('partner-inf.html' , 'partner-inf' , {} , {title : home_info.cooperative_partner[index].title , body : home_info.cooperative_partner[index].body});
				partner_inf.addEventListener('loaded' , function(){
					partner_inf.show('pop-in'); 
					plus.nativeUI.closeWaiting();
				})
			})
		})
		
	}
}
